package com.hks360.library.util;

import java.util.Iterator;
import java.util.List;

public class StringUtil {

    public static String join(Iterator iterator, String separator) {

        // handle null, zero and one elements before building a buffer
        if (iterator == null) {
            return null;
        }
        if (!iterator.hasNext()) {
            return "";
        }
        Object first = iterator.next();
        if (!iterator.hasNext()) {
            return first.toString();
        }

        // two or more elements
        StringBuffer buf = new StringBuffer(256);
        if (first != null) {
            buf.append(first);
        }

        while (iterator.hasNext()) {
            if (separator != null) {
                buf.append(separator);
            }
            Object obj = iterator.next();
            if (obj != null) {
                buf.append(obj);
            }
        }
        return buf.toString();
    }

    public static <T> String join(List<T> list, String separator) {
        if (list == null) {
            return "";
        }
        return join(list.iterator(), separator);
    }

    public static String join(int[] ids, String separator) {
        if (ids == null || ids.length <= 0) {
            return "";
        }
        int first = ids[0];
        if (ids.length == 1) {
            return String.valueOf(first);
        }

        StringBuffer buf = new StringBuffer(256);
        buf.append(first);
        for (int i = 1; i < ids.length; i++) {
            buf.append(separator).append(ids[i]);
        }
        return buf.toString();
    }

    public static String join(String[] strs, String separator) {
        if (strs == null || strs.length <= 0) {
            return "";
        }
        String first = strs[0];
        if (strs.length == 1) {
            return String.valueOf(first);
        }

        StringBuffer buf = new StringBuffer(256);
        buf.append(first);
        for (int i = 1; i < strs.length; i++) {
            buf.append(separator).append(strs[i]);
        }
        return buf.toString();
    }

    public static String trim(String str) {
        if (str == null) {
            return "";
        } else {
            return str.trim();
        }
    }

    public static String replaceImgTag(String str) {
        return str.replaceAll("<img.+?>", "");
    }

    public static boolean isEmpty(String str) {
        if (str == null || "".equals(str)) {
            return true;
        }
        return false;

    }
}
